package Function;

import FCRendering.FCRConstants;
import Function.Utils.FCFileCopyToMaterial;
import VWorldGameStudio.FCAnimation;
import lombok.Getter;
import lombok.Setter;
import utils.FCDataSingles;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Vector;

public class FCAnimationLibrary {

    @Getter
    private ArrayList<String> animationId = new ArrayList<String>();// 攻击动画id
    private ArrayList<FCAnimation> animationList = new ArrayList<FCAnimation>();// 攻击动画数据
    private FCFileCopyToMaterial fileCopy = new FCFileCopyToMaterial();
    private static final String ANIMATION_LIST_PATH = "./data/animationlist.vw";
    private static final String ANIMATIONS_PATH = "./data/animation";
    private int animationallnum = 0;

    /**
     * 初始化动画类，读取数量
     * 建议游戏启动方使用
     *
     * @date 2021-11-30
     */
    public FCAnimationLibrary() {
        try {
            this.animationallnum = new File(ANIMATIONS_PATH).listFiles().length;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 动画数据加载函数
     *
     * @date 2021-11-30
     */
    public void loadAnimationData() {
        try {
            File[] f = new File(ANIMATIONS_PATH).listFiles();
            for (int i = 0; i < animationallnum; i++) {
                FCAnimation animation = FCDataSingles.getReader().readAnimationData(f[i].getPath());
                animation.setId(f[i].getName().replace(".vw",""));
                animationList.add(animation);
                animationId.add(animation.getID());
                System.out.println(animation.getID()+" Fly:"+animation.getFlyAniForWrite()+" Boom:"+animation.getBoomAniForWrite()+" Fire:"+animation.getFireAniForWrite());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取动画实际数据量
     *
     * @return integer
     * @date 2021-11-30
     */
    public int getAnimationAllNum() {
        return this.animationallnum;
    }




    // 注册攻击动画
    public void registerAnimation(boolean isnew, FCAnimation ani) {
        if(animationId.contains(ani.getID())) {
            animationList.remove(animationId.indexOf(ani.getID()));
            animationId.remove(ani.getID());
        }
        animationList.add(ani);
        animationId.add(ani.getID());
        FCDataSingles.getWriter().writeAnimationData(ani);
    }

    // 根据id获取data
    public FCAnimation getAnimationOfID(String id) {
        if(id == null) return null;
        if(id.equals("null")) return null;
        int num = animationId.indexOf(id);
        if(FCRConstants.DEBUG_MODE) {
            FCAnimation animation = FCDataSingles.getReader().readAnimationData(ANIMATIONS_PATH + "/" + id + ".vw");
            return animation;
        }
        return (num > -1) ? animationList.get(num) : null;
    }

    // 获取数量
    public int getAnimationNum() {
        return animationList.size();
    }

    // 检测是否重复
    public boolean isRepeat(String id) {
        for (String s : animationId)
            if (s.equals(id))
                return true;
        return false;
    }

    // 根据编号获取id
    public String getIDOfNum(int num) {
        return animationId.get(num);
    }

    // 获取描述根据num
    public String getInfoOfNum(int num) {
        FCAnimation ani = animationList.get(num);
        StringBuilder sb = new StringBuilder();
        sb.append("id:" + ani.getID());
        sb.append(" 类型:" + ani.getType());
        sb.append(" 开头动画:" + ani.getFireAniForWrite());
        sb.append(" 中间动画:" + ani.getFlyAniForWrite());
        sb.append(" 中间动画2:" + ani.getFlyAniForWrite2());
        sb.append(" 结束动画:" + ani.getBoomAniForWrite());
        return sb.toString();
    }
}
